package com.platform.common.core.utils;

import com.platform.common.core.domain.entity.GjcMawb;

import java.lang.reflect.Field;

/**
 * @Description: 对比对象值是否一致
 * @Author: caoliu
 * @Date: 2024/9/18 17:10
 **/
public class ReflectionObjectUtil {


    /**
     * 对象值对比--以source的字段为主，如果source有字段但是target则以source为主
     * @param source
     * @param target
     * @return
     * @throws IllegalAccessException
     */
    public static boolean areObjectsEqual(Object source, Object target) throws IllegalAccessException {
        if (source == null && target == null) {
            return true;
        }
        if (source == null || target == null) {
            return false;
        }
        Class<?> clazz = source.getClass();
        Field[] fields = clazz.getDeclaredFields();
        for (Field field : fields) {
            field.setAccessible(true);
            Object sourceValue = field.get(source);
            Object targetValue = field.get(target);
            if (sourceValue == null && targetValue == null) {
                continue;
            }
            if (sourceValue!=null&&targetValue!=null
                    &&!sourceValue.equals(targetValue)) {
                    return false;
                }
            if (sourceValue!=null&&targetValue==null) {
                    return true;
                }
            }

        return true;
    }

    public static void main(String[] args) throws IllegalAccessException {
        GjcMawb person1 = new GjcMawb();
        person1.setPc(1l);
        person1.setWeight(Double.valueOf("100.00"));
//        person1.setCargoType("1112");
        GjcMawb person2 = new GjcMawb();
        person2.setPc(1l);
        person2.setWeight(Double.valueOf("100.00"));
        person2.setCargoType("111");

        boolean result = areObjectsEqual(person1, person2);
        System.out.println(result);
    }
}
